<template>
  <view>
    <!-- 搜索模块 -->
    <ugo-search></ugo-search>
    <!-- 主体区域 -->
    <view class="m-category">
      <scroll-view scroll-y class="left" >
        <view class="item" @click="changeRightShow(index)" v-for="(item,index) in catLeft" :key="item.cat_id" :class="{active:index === activeIndex}">{{ item.cat_name }}</view>
      </scroll-view>
      <scroll-view scroll-y class="right" :scroll-top="scrollTop">
        <view class="cate" v-for="item in catRight" :key="item.cat_id">
          <view class="title">{{ item.cat_name }}</view>
          <view class="goods-list">
            <navigator class="goods" v-for="goods in item.children" :key="goods.cat_id" :url="`/pages/goods-list/index?query=${goods.cat_name}&cid=${goods.cat_id}`">
              <image
                :src="goods.cat_icon"
                class="pic"
                mode="aspectFit"
              />
              <view class="name">{{ goods.cat_name }}</view>
            </navigator>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
export default {
    name: 'HeimaUgoIndex',
    data() {
        return {
            scrollTop:0,
            catList:[],
            activeIndex:0,
        };
    },
    onLoad(){
        this.getCatList()
    },
    computed:{
        catLeft(){
            return this.catList.map(item=>{
                return {
                cat_id:item.cat_id,
                cat_name:item.cat_name
                }
            })
        },
        catRight(){
            return this.catList[this.activeIndex]?.children
        }
    },
    methods:{
        //获取分类列表函数
     async getCatList(){
            const res = await uni.$u.http.get('/categories')
            // console.log(res ,'111');
            this.catList = res.data.message
        },
        //点击切换右侧商品显示的函数
        changeRightShow(index){
            this.activeIndex = index
            //因为小程序是双线程的,所以他需要调用setdata来改变渲染层中的值，由于他的优化编译的时候他检测不到值的变化就不会调用setdata，我们需要在他编译前判断一下当前scrolltop的值是0还是-1，根据当前值让他发生改变，那他在编译的时候就会调用setdata方法
            this.scrollTop = this.scrollTop ? 0 : -1
        }
    }
};
</script>

<style lang="scss">
.m-category {
  display: flex;
  height: calc(100vh - 20rpx - 34px);
  .left {
    height: 100%;
    width: 182rpx;
    font-size: 28rpx;
    text-align: center;
    background-color: #f3f3f3;
    .item {
      padding: 17rpx 0;
      &.active {
        background: #fff;
        color: #ea4350;
        position: relative;
        &::before {
          content: "";
          position: absolute;
          left: 0;
          width: 4rpx;
          height: 44rpx;
          background: #ea4350;
        }
      }
    }
  }
  .right {
    height: 100%;
    flex: 1;
    .title {
      margin: 18rpx 0 21rpx;
      text-align: center;
    }
  }
}
.goods-list {
  display: flex;
  padding: 0 48rpx;
  justify-content: flex-start;
  flex-wrap: wrap;
  .goods{
      margin-right: 57rpx;
    &:nth-child(3n){
        margin-right: 0;
    }
      .pic {
        width: 120rpx;
        height: 120rpx;
      }
      .name {
        margin-bottom: 30rpx;
        font-size: 21rpx;
        color: #2e2e2e;
        text-align: center;
        line-height: 1;
      }
  }
}
</style>